//
//  SchoolSelectViewController.m
//  HotWord
//
//  Created by 贺 欢 on 13-3-3.
//
//

#import "SchoolSelectViewController.h"
#import "SchoolSelectCell.h"
#import "HeaderTitleView.h"

@interface SchoolSelectViewController ()
{
    NSArray *_schools;
    NSInteger _lastSelectedRow;
}
@property (retain, nonatomic) IBOutlet UITableView *schoolTableView;

@end

@implementation SchoolSelectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (id)initWithSchools:(NSArray *)schools
{
    self = [super init];
    if (self)
    {
        _schools = [schools retain];
    }
    return self;
}


- (void)dealloc
{
    [_schoolTableView release];
    [_schools release];
    [_headerTitle release];
    [super dealloc];
}

- (void)viewDidUnload
{
    [self setSchoolTableView:nil];
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _schoolTableView.top = self.view.height;
    _lastSelectedRow = -1;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.view viewWithTag:10].alpha = 0;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.4
                     animations:^{
                         _schoolTableView.top = 0;
                         [self.view viewWithTag:10].alpha = 1;
                     }];
}

#pragma mark - IBAction

- (IBAction)tapOnBackBtn:(id)sender
{
    NSIndexPath *selectIndexPath = [_schoolTableView indexPathForSelectedRow];
    if (selectIndexPath && _delegate && [_delegate respondsToSelector:@selector(schoolSelectViewController:didSelectSchool:)])
    {
        NSDictionary *schoolDic = [_schools objectAtIndex:selectIndexPath.row];

        [_delegate schoolSelectViewController:self didSelectSchool:[schoolDic objectForKey:@"chineseName"]];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_schools count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 34;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderTitleView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderTitleView" owner:self options:nil] objectAtIndex:0];
    headerView.titleLabel.text = _headerTitle;
    
    return headerView;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SchoolSelectCell";
    SchoolSelectCell *cell = (SchoolSelectCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SchoolSelectCell" owner:self options:nil] objectAtIndex:0];
        cell.type = SchoolSelectCellTypeSchoolSelect;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSDictionary *schoolDic = [_schools objectAtIndex:indexPath.row];
    [cell setTitle:[schoolDic objectForKey:@"chineseName"]];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_lastSelectedRow != indexPath.row)
    {
        _lastSelectedRow = indexPath.row;
    }
    else
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        _lastSelectedRow = -1;
    }

}





@end
